﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace XinHeWechat.Common
{
    public class LogHelper
    {
        /// <summary>
        /// 调试用的log记录
        /// </summary>
        /// <param name="msg"></param>
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static void WriteDebugLog(string msg)
        {
            string strLogPath = Config.LogPath;
            string strPath = HttpContext.Current.Server.MapPath(strLogPath + "debug/");
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }

            string url = HttpContext.Current.Request.Url.ToString();
            string formData = "";
            for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
            {
                if (HttpContext.Current.Request.Form.Keys[i].ToString().Substring(0, 1) != "_")
                {
                    string key = HttpContext.Current.Request.Form.Keys[i].ToString();
                    string value = HttpContext.Current.Request.Form[i].ToString();
                    formData += (" " + key + "=" + value + " ,");
                }
            }
            formData = formData.Trim().Trim(',');

            using (StreamWriter sw = File.AppendText(strPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".Log"))
            {
                sw.WriteLine("---------------------------------------------------------------------------------------");
                sw.WriteLine();
                sw.WriteLine("Time:\t" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                sw.WriteLine("Msg:\t" + msg);
                sw.WriteLine("Url:\t" + url);
                sw.WriteLine("Form:\t" + formData);
                sw.WriteLine();
            }
        }

        /// <summary>
        /// webapi过滤器捕捉的错误
        /// </summary>
        /// <param name="msg"></param>
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static void WriteWebApiLog(Exception ex)
        {
            string strLogPath = Config.LogPath;
            string strPath = HttpContext.Current.Server.MapPath(strLogPath + "exception/");
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }

            string url = HttpContext.Current.Request.Url.ToString();
            string formData = "";
            for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
            {
                if (HttpContext.Current.Request.Form.Keys[i].ToString().Substring(0, 1) != "_")
                {
                    string key = HttpContext.Current.Request.Form.Keys[i].ToString();
                    string value = HttpContext.Current.Request.Form[i].ToString();
                    formData += (" " + key + "=" + value + " ,");
                }
            }
            formData = formData.Trim().Trim(',');

            using (StreamWriter sw = File.AppendText(strPath + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".Log"))
            {
                sw.WriteLine("---------------------------------------------------------------------------------------");
                sw.WriteLine();
                sw.WriteLine(" Time:\t" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                sw.WriteLine("  Url:\t" + url);
                sw.WriteLine(" Form:\t" + formData);
                sw.WriteLine(" Type:\t" + ex.GetType());
                sw.WriteLine("  Msg:\t" + ex.Message);
                sw.WriteLine("Trace:\t" + ex.StackTrace);
                sw.WriteLine();
            }
        }
    }
}
